package cc.mycraft.pvp;

import java.util.HashMap;
import java.util.Random;
import java.util.Set;

import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

public class SafeArea implements Listener{
	public static double posX1;
	public static double posX2;
	public static double posZ1;
	public static double posZ2;
	public static int AposX;
	public static int AposY;
	public static int AposZ;
	public static int Range;
	public static HashMap<Player,Boolean> Gods = new HashMap<Player,Boolean>();
	
	public static void run() {
		posX1 = Main.config.getDouble("SafeArea.X1");
		posX2 = Main.config.getDouble("SafeArea.X2");
		posZ1 = Main.config.getDouble("SafeArea.Z1");
		posZ2 = Main.config.getDouble("SafeArea.Z2");
		AposX = Main.config.getInt("JoinArenaPos.X");
		AposY = Main.config.getInt("JoinArenaPos.Y");
		AposZ = Main.config.getInt("JoinArenaPos.Z");
		Range = Main.config.getInt("JoinArenaPos.Range");
	}
	public static boolean addGodMode(Player p) {
		if(isInSafeArea(p)) {
			Gods.replace(p,true);
			p.sendMessage("§e进入安全区，已开启安全模式。注意，安全模式不能防止debuff和buff。");
		} else {
			p.sendMessage("§c在安全区内才能进入安全模式。");
		}
		return true;
	}
	public static boolean removeGodMode(Player p) {
		p.sendMessage("§c关闭安全模式。");
		Gods.replace(p,false);
		return true;
	}
	//加入站场，50行的检测是为了防止玩家在战场内传送。
	public static boolean joinArena(Player p) {
		if(!isInSafeArea(p)) {
			p.sendMessage("§e你已经在战场里了。");
			return true;
		}
		//菜鸡获得1分钟额外4颗黄心
		if(PVPListener.Chicken.get(p)>=3) {
			p.sendMessage("§a§l你获得了菜鸡之心增益！开局获得额外4颗黄心！");
			p.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 1200, 1, false));
		} 
		p.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 100, 10, false));
		p.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 100, 10, false));
		p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 100, 3, false));
		p.setFoodLevel(20);
		p.setHealth(p.getMaxHealth());
		Random r = new Random();
		Location l = new Location(p.getWorld(), AposX-Range+r.nextInt(2*Range), AposY, AposZ-Range+r.nextInt(2*Range));
		p.teleport(l);
		Gods.replace(p, false);
		ActionBar b = new ActionBar("§e你加入了战斗，安全模式已关闭。碾碎敌人吧！");
		b.send(p);
		return true;
	}
	//设置补给箱物品，击杀玩家后掉落一个补给箱。
	public static boolean setChest(Player p) {
		Main.getPlugin().getConfig().set("ChestItem", p.getItemInHand());
		Main.getPlugin().saveConfig();
		p.sendMessage("已将手中物品设置为补给箱。");
		return true;
	}
	public static boolean isInSafeArea(Player p) {
		return p.getLocation().getX()>posX1&&p.getLocation().getX()<posX2&&p.getLocation().getZ()>posZ1&&p.getLocation().getZ()<posZ2;
	}
}
